package com.songshuai.sales.tool.controller;

import com.songshuai.sales.tool.dto.request.sopmanage.CustomerPushReqDto;
import com.songshuai.sales.tool.dto.request.sopmanage.CustomerQueryReqDto;
import com.songshuai.sales.tool.dto.request.sopmanage.DynamicQueryReqDto;
import com.songshuai.sales.tool.dto.request.sopmanage.SopManageListReqDto;
import com.songshuai.sales.tool.dto.response.sopmanage.*;
import com.songshuai.sales.tool.service.SopManageService;
import com.songshuai.sales.tool.util.PageUtil;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@Api("SOP管理")
@RestController
@RequestMapping("/api/sopManage/")
public class SopManageController {

    @Autowired
    private SopManageService sopManageService;

    @ApiOperation(value = "动态查询推送记录")
    @PostMapping("dynamicQuery")
    public QueryRes dynamicQuery(@RequestBody BaseParmDto<DynamicQueryReqDto> request) {
        try {
            PageUtil pageUtil = sopManageService.dynamicQuery(request);
            List<DynamicQueryResDto> dynamicQueryResDtoList = (List<DynamicQueryResDto>) pageUtil.getLists();
            return QueryPageRes.setQueryResData(dynamicQueryResDtoList, pageUtil.getTotalSize() == null ? 0L : pageUtil.getTotalSize().longValue());

        } catch (Exception ex) {
            log.error("api/sopManage/dynamicQuery", ex);
            throw new BusinessException(ex.getMessage(), ResultStatus.FAILOfficial);
        }

    }
    @ApiOperation("动态查询sop展示结果")
    @PostMapping("selectSopManageList")
    public QueryRes selectSopManageList(@RequestBody BaseParmDto<SopManageListReqDto> request){
        try{
            PageUtil pageUtil=sopManageService.selectSopManageList(request);
            List<SopManageListResDto> sopManageListResDto=(List<SopManageListResDto>) pageUtil.getLists();
            return QueryPageRes.setQueryResData(sopManageListResDto,pageUtil.getTotalSize() == null ? 0L : pageUtil.getTotalSize().longValue());
        }catch (Exception e){
            log.error("api/sopManage/selectSopManageList", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    @ApiOperation("根据多条件查询用户行为记录集合")
    @PostMapping("queryByMsg")
    public QueryRes selectByMsg(@RequestBody BaseParmDto<CustomerQueryReqDto> request) {
        try {
            PageUtil page = sopManageService.selectByMsg(request);
            List<CustomerActResDto> lists = (List<CustomerActResDto>) page.getLists();
            return QueryPageRes.setQueryResData(lists, page.getTotalSize() == null ? 0L : page.getTotalSize().longValue());
        } catch (Exception e) {
            log.error("/api/sopManage/queryByMsg", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

    @ApiOperation("根据客户id查询sop行为数据")
    @GetMapping("querySopActList")
    public QueryRes querySopActList(@RequestParam(value = "actionUser") String actionUser) {
        try {
            List<SopActResDto> sopActResDtoList = sopManageService.selectByActionUser(actionUser);
            return QueryRes.setResData(sopActResDtoList);
        } catch (Exception e) {
            log.error("/api/sopManage/querySopActList", e);
            throw new BusinessException(ResultStatus.ERROR_RECORD_NOT_FOUND);
        }
    }

    @ApiOperation("sop预览展示")
    @GetMapping("showSopPreView")
    public QueryRes<SopPreviewResDto> showSopPreView(@RequestParam(value = "sopId")String sopId){
        try {
            SopPreviewResDto sopPreviewResDto = sopManageService.showSopPreView(sopId);
            return QueryRes.setResData(sopPreviewResDto);
        } catch (Exception e) {
            log.error("/api/sopManage/showSopPreView",e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

    @ApiOperation("查询客户绑定小程序状态")
    @GetMapping("queryBindStatus")
    public QueryRes queryBindStatus(@RequestParam("customerIds") String customerIds) {
        try {
            Map map = sopManageService.queryBindStatus(customerIds);
            return QueryRes.setResData(map);
        } catch (Exception e) {
            log.error("/api/sopManage/queryCustomerList", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

    @ApiOperation("查询当前客户的Sop推送记录")
    @GetMapping("querySopPushRecord")
    public QueryRes<List<SopPushRecordResDto>> querySopPushRecord(@RequestParam(value = "reciveUser") String reciveUser) {
        try {
            List<SopPushRecordResDto> sopPushRecordList = sopManageService.selectSopPushRecord(reciveUser);
            return QueryRes.setResData(sopPushRecordList);
        } catch (Exception e) {
            log.error("/api/sopManage/querySopPushRecord",e);
            throw new BusinessException(ResultStatus.ERROR_RECORD_NOT_FOUND);
        }
    }

    @ApiOperation("批量推送")
    @PostMapping("pushCustomerList")
    public QueryRes<SuccessResDto> pushCustomerList(HttpServletRequest request, @RequestBody List<CustomerPushReqDto> customerPushReqDtoList){
        try {
            String authorization = request.getHeader("Authorization");
            SuccessResDto successResDto = sopManageService.insertPushRecord(customerPushReqDtoList, authorization);
            return QueryRes.setResData(successResDto);
        } catch (Exception e) {
            log.error("/api/sopManage/pushCustomerList",e);
            throw new BusinessException(ResultStatus.ERROR_CREATE_FAILED);
        }
    }
    @ApiOperation("获取当前登陆人角色内容列表")
    @GetMapping("getAllRole")
    public QueryRes<List<Integer>> getALLRole(){
        return QueryRes.setResData(sopManageService.getAllRole());
    }
}
